package com.tools.scanner.ui.qrcode.creator.impl

import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import com.google.mlkit.vision.barcode.common.Barcode
import com.tools.scanner.R
import com.tools.scanner.databinding.FragmentWifiInputBinding
import com.tools.scanner.utils.ToastUtil

class CreateWifiFragment : BaseCreateFragment() {

    private lateinit var mBinding: FragmentWifiInputBinding

    private var wifiModel = Barcode.WiFi.TYPE_WPA

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentWifiInputBinding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.llModel.setOnClickListener {
            showPopupWindow()
        }
    }

    private fun showPopupWindow() {
        val inflater = LayoutInflater.from(requireActivity())
        val popupView: View = inflater.inflate(R.layout.popup_wifi, null)

        val popupWindow = PopupWindow(
            popupView,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )

        popupWindow.isFocusable = true

        popupView.findViewById<TextView>(R.id.tv_1).let {
            if (wifiModel == Barcode.WiFi.TYPE_WPA) {
                it.setTextColor(Color.parseColor("#FFD55C58"))
            }
            it.setOnClickListener {
                popupWindow.dismiss()
                mBinding.tvModel.text = (it as TextView).text
                wifiModel = Barcode.WiFi.TYPE_WPA
            }
        }

        popupView.findViewById<TextView>(R.id.tv_2).let {
            if (wifiModel == Barcode.WiFi.TYPE_WEP) {
                it.setTextColor(Color.parseColor("#FFD55C58"))
            }

            it.setOnClickListener {
                popupWindow.dismiss()
                mBinding.tvModel.text = (it as TextView).text
                wifiModel = Barcode.WiFi.TYPE_WEP
            }
        }

        popupView.findViewById<TextView>(R.id.tv_3).let {
            if (wifiModel == Barcode.WiFi.TYPE_OPEN) {
                it.setTextColor(Color.parseColor("#FFD55C58"))
            }

            it.setOnClickListener {
                popupWindow.dismiss()
                mBinding.tvModel.text = (it as TextView).text
                wifiModel = Barcode.WiFi.TYPE_OPEN
            }
        }

        popupWindow.setOnDismissListener {
            mBinding.ivMore.rotation = 0f
        }

        val offsetY = resources.getDimension(R.dimen.dp_5)

        popupWindow.showAsDropDown(mBinding.llModel, 0, offsetY.toInt())

        mBinding.ivMore.rotation = 180f
    }

    override fun checkParams(): Boolean {
        if (TextUtils.isEmpty(mBinding.etName.text.toString())) {
            ToastUtil.showToast(R.string.input_type_check_ssid)
            return false
        }

        when (wifiModel) {
            Barcode.WiFi.TYPE_WPA, Barcode.WiFi.TYPE_WEP -> {
                if (TextUtils.isEmpty(mBinding.etPassword.text.toString())) {
                    ToastUtil.showToast(R.string.input_type_check_pwd)
                    return false
                }
            }
        }

        return true
    }

    override fun createQrCodeContent(): String {
        return when (wifiModel) {
            Barcode.WiFi.TYPE_WPA -> {
                "WIFI:T:WPA;S:${mBinding.etName.text};P:${mBinding.etPassword.text};H:;"
            }

            Barcode.WiFi.TYPE_WEP -> {
                "WIFI:T:WEP;S:${mBinding.etName.text};P:${mBinding.etPassword.text};H:;"
            }

            else -> {
                "WIFI:T:nopass;S:${mBinding.etName.text};P:${mBinding.etPassword.text};H:;"
            }
        }
    }
}